# 编译器配置
export CC       = gcc
export CXX      = g++
export CFLAGS   = -std=c99 -Wall -Wextra -g
export CXXFLAGS = -std=c++17 -Wall -Wextra -g
export LDFLAGS  = 

# 目录配置
export SRC_ROOT  = $(abspath src)
export BUILD_DIR = $(abspath build)
export BIN_DIR   = $(abspath bin)
export LIB_DIR   = $(abspath lib)
SRC_DIR          = src
MAIN             = $(SRC_DIR)/main.cpp
export INCLUDE_DIRS     = $(abspath $(shell find $(SRC_DIR) -type d -name "include"))
TARGET           = $(BIN_DIR)/main
SUBDIRS         := $(shell find $(SRC_DIR) -mindepth 1 -maxdepth 1 -type d )
# LIBS             = $(patsubst %,$(LIB_DIR)/%.a,$(notdir $($(filter-out %src/util, $(SUBDIRS)))))

all: sub_targets $(TARGET)

# 创建必要目录
dirs:
	@echo "Creating directories..."
	@mkdir -pv $(BUILD_DIR) $(BIN_DIR) $(LIB_DIR)

lib: $(SUBDIRS) | dirs
	make -C $< lib 

$(TARGET): lib
	$(CXX) $(CXXFLAGS) $(patsubst %,-I%,$(INCLUDE_DIRS)) $(MAIN) $(wildcard $(LIB_DIR)/*) -o $@ $(LDFLAGS)
	@echo -e "=== \e[38;5;82mBuild successful\e[0m -> \e[38;5;202m$(TARGET)\e[0m ==="

sub_targets: $(SUBDIRS) | dirs
	make -C $<

# 清理命令
clean:
	@echo "Cleaning build artifacts..."
	@rm -rfv $(BUILD_DIR) $(LIB_DIR) $(BIN_DIR)

.PHONY: clean
